package com.baiyi.cloud.platform.tools.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baiyi.cloud.platform.commons.PageResult;
import com.baiyi.cloud.platform.commons.Result;
import com.baiyi.cloud.platform.tools.model.SysBwIp;
import com.baiyi.cloud.platform.tools.service.SysBwIpService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 *
 * @author huwenfeng
 * @email hwf@imlaidian.com
 * @date 2019-01-25 16:42:24
 */
@RestController
@RequestMapping("sysbwip")
@Api(tags = "黑名单")
public class SysBwIpController {

    @Autowired
    private SysBwIpService sysBwIpService;

    
    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('tools:sysbwip:list')")
    public PageResult<?> list(@RequestParam Map<String, Object> params){
        PageResult<?> pageResult = sysBwIpService.findAll(params);
        return pageResult;
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    @PreAuthorize("hasAnyAuthority('tools:sysbwip:save')")
    public Result save(@RequestBody SysBwIp sysBwIp){
			sysBwIpService.save(sysBwIp);

        return Result.succeed("");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @PreAuthorize("hasAnyAuthority('tools:sysbwip:update')")
    public Result update(@RequestBody SysBwIp sysBwIp){
			sysBwIpService.update(sysBwIp);

        return Result.succeed("");
    }
    
    
    /**
	 * IP新增或者更新
	 * @param sysPermission
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('tools:put/sysbwip','tools:post/sysbwip')")
	@PostMapping("/saveOrUpdate")
	public Result saveOrUpdate(@RequestBody SysBwIp sysBwIp) {
		try{
			if (sysBwIp.getId()!=null){
				sysBwIpService.update(sysBwIp);
			}else {
				sysBwIpService.save(sysBwIp);
			}
			return Result.succeed("操作成功");
		}catch (Exception ex){
			return Result.failed("操作失败");
		}
	}

    /**
     * 删除
     */
    /*@RequestMapping("/delete")
    @PreAuthorize("hasAnyAuthority('tools:sysbwip:delete')")*/
    @PreAuthorize("hasAuthority('tools:delete/ip/{id}')")
	@ApiOperation(value = "后台管理删除黑名单IP")
	@DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
			sysBwIpService.delete(id);
        return Result.succeed("");
    }

}
